﻿using System;
using System.IO;
using System.Diagnostics;

namespace GameServer
{
    class Log
    {
        private static StreamWriter _LogFile;

        public static bool setup(string logFile)
        {
            try
            {
                _LogFile = new StreamWriter(logFile, true);
                _LogFile.WriteLine("Start up: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());
                _LogFile.Flush();
            }
            catch { }

            return false;
        }

        public static void setTitle(string sTitle)
        {
            Console.Title = sTitle;
        }

        public static void closeLog()
        {
            try { if (_LogFile != null) { _LogFile.Close(); } }
            catch { }
        }

        public static void WriteLine(string sMsg)
        {
            WriteLine(sMsg, ConsoleColor.Green, ConsoleColor.DarkGreen);
        }

        public static void WriteError(string sMsg)
        {
            WriteLine(sMsg, ConsoleColor.Red, ConsoleColor.DarkRed);
        }

        public static void WriteDebug(string sMsg)
        {
            WriteLine(sMsg, ConsoleColor.Magenta, ConsoleColor.DarkMagenta);
        }

        public static void WriteBlank()
        {
            Console.WriteLine("");
        }

        public static void WriteLine(string sMsg, ConsoleColor c1, ConsoleColor c2)
        {
            DateTime _DTN = DateTime.Now;
            StackFrame _SF = new StackTrace().GetFrame(2);
            Console.Write("[" + _DTN.ToShortDateString() + " " + _DTN.ToLongTimeString() + "] [");
            Console.ForegroundColor = c1;
            Console.Write(_SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("] » ");
            Console.ForegroundColor = c2;
            Console.WriteLine(sMsg);
            Console.ForegroundColor = ConsoleColor.Gray;

            try {
                if (_LogFile != null) {
                    _LogFile.WriteLine("[" + _DTN.ToShortDateString() + " " + _DTN.ToLongTimeString() + "] [" + _SF.GetMethod().ReflectedType.Name + "." + _SF.GetMethod().Name + "] » " + sMsg);
                    _LogFile.Flush();
                }
            }
            catch { }
            
        }

        public static void WritePlain(string sMsg)
        {
            Console.WriteLine(sMsg);
            try
            {
                if (_LogFile != null)
                {
                    _LogFile.WriteLine(sMsg);
                    _LogFile.Flush();
                }
            }
            catch { }
        }


        public static void WriteFile(string sMsg)        {
            try
            {
                if (_LogFile != null)
                {
                    _LogFile.WriteLine(sMsg);
                    _LogFile.Flush();
                }
            }
            catch { }
        }
    }
}
